<?xml version="1.0" encoding="UTF-8"?>
<!--
	Application context definition for PetClinic on Hibernate.
-->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
		xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="
			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
			http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<!-- ========================= RESOURCE DEFINITIONS ========================= -->

	<!-- Configurer that replaces ${...} placeholders with values from a properties file -->
	<!-- (in this case, JDBC-related settings for the dataSource definition below) -->
	<context:property-placeholder location="classpath:jdbc.properties"/>

	<!--
		Uses Apache Commons DBCP for connection pooling. See Commons DBCP documentation
		for the required JAR files. Alternatively you can use another connection pool
		such as C3P0, similarly configured using Spring.
	-->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
			p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}"
			p:password="${jdbc.password}"/>

	<!-- JNDI DataSource for JEE environments -->
	<!--
		<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/petclinic"/>
	-->

	<!-- Hibernate SessionFactory -->
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"
			p:dataSource-ref="dataSource" p:mappingResources="petclinic.hbm.xml">
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
				<prop key="hibernate.generate_statistics">${hibernate.generate_statistics}</prop>
			</props>
		</property>
		<property name="eventListeners">
			<map>
				<entry key="merge">
					<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
				</entry>
			</map>
		</property>
	</bean>

	<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
			p:sessionFactory-ref="sessionFactory"/>

	<!-- Transaction manager that delegates to JTA (for a transactional JNDI DataSource) -->
	<!--
	<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
	-->


	<!-- ========================= BUSINESS OBJECT DEFINITIONS ========================= -->

	<!--
		Activates various annotations to be detected in bean classes:
		Spring's @Required and @Autowired, as well as JSR 250's @Resource.
	-->
	<context:annotation-config/>

	<!--
		Instruct Spring to perform declarative transaction management
		automatically on annotated classes.
	-->
	<tx:annotation-driven/>

	<!--
		Exporter that exposes the Hibernate statistics service via JMX. Autodetects the
		service MBean, using its bean name as JMX object name.
	-->
	<context:mbean-export/>

	<!-- PetClinic's central data access object: Hibernate implementation -->
	<bean id="clinic" class="org.springframework.samples.petclinic.hibernate.HibernateClinic"/>

	<!-- Hibernate's JMX statistics service -->
	<bean name="petclinic:type=HibernateStatistics" class="org.hibernate.jmx.StatisticsService" autowire="byName"/>

</beans>
